package com.sds.replicator.transformers;

import java.io.UnsupportedEncodingException;

/**
 * Created by IntelliJ IDEA.
 * User: sdsbirdman
 * Date: 7/3/11
 * Time: 8:52 PM
 * To change this template use File | Settings | File Templates.
 */
public class JSTransformer implements Transformer {
    private String localPath;

    public JSTransformer(String localPath) {
        this.localPath = localPath;

    }

    @Override
    public String getContentType() {
        return "application/x-javascript";
    }

    @Override
    public byte[] transformContent(String encoding, byte[] content) throws UnsupportedEncodingException {
        String s = new String(content, encoding);
        s = s.replaceAll("'/ajax/", "'" + localPath + "?url=/ajax/");
        s = s.replaceAll("f=document.location.protocol", "f='http://fb-connect-vn.appspot.com/fb?url='+document.location.protocol");

//        s = s.replaceAll("http://", localPath + "?url=http://");
//        s = s.replaceAll("https://", localPath + "?url=https://");
        return s.getBytes(encoding);
    }
}
